const Model = require('../models/hanziStrokeModel')

const getAllHanziStrokes = async (ctx) => {
  ctx.body = { success: true, data: await Model.getAllHanziStrokes() }
}

const getHanziStrokeById = async (ctx) => {
  const { id } = ctx.params
  const hanziStroke = await Model.getHanziStrokeById(id)
  if (!hanziStroke) ctx.throw(404, 'Hanzi Stroke not found')
  ctx.body = hanziStroke
}

const createHanziStroke = async (ctx) => {
  const strokeData = ctx.request.body
  const strokeId = await Model.createHanziStroke(strokeData)
  ctx.status = 201
  ctx.body = { id: strokeId, ...strokeData }
}

const updateHanziStroke = async (ctx) => {
  const { id } = ctx.params
  const strokeData = ctx.request.body
  await Model.updateHanziStroke(id, strokeData)
  ctx.body = { success: true }
}

const deleteHanziStroke = async (ctx) => {
  const { id } = ctx.params
  await Model.deleteHanziStroke(id)
  ctx.status = 204
}

module.exports = {
  getAllHanziStrokes,
  getHanziStrokeById,
  createHanziStroke,
  updateHanziStroke,
  deleteHanziStroke,
}